/**
 * 0. Project	: com.dnbns.bodywater 프로젝트
 *
 * 1. FileName	: SettingActivity.java
 * 2. Package	: com.dnbns.bodywater
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 11. 15. 오전 10:49:15
 * 5. Comment	: 
 */

package com.dnbns.bodywater;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dnbns.common.AlarmSchedule;
import com.dnbns.common.StaticValue;
import com.dnbns.lib.CodeUtil;
import com.dnbns.lib.RbPreference;

public class SettingActivity extends FragmentActivity implements WeightDialogFragment.NoticeDialogListener {

	private float mWeight;
	private String mBirth;
	private boolean isSexFlag;
	private boolean isAlarmFlag;
	private boolean isVibrateFlag;
	private RelativeLayout alarm_setting;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		init();
		initTop();
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		getData();
	}
	
/**
 ***************************************************************
 * init
 ***************************************************************
 */
	
	//초기화.
	private void init(){
		RelativeLayout birth_layout = (RelativeLayout)findViewById(R.id.birth_layout);
		RelativeLayout weight_layout = (RelativeLayout)findViewById(R.id.weight_layout);
		
		birth_layout.setOnClickListener(OCL);
		weight_layout.setOnClickListener(OCL);
	}
	
	private void initTop(){
		ImageView back = (ImageView)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	
	//DB에서 데이터 select.
	private void getData(){
		RbPreference pref = new RbPreference(getApplicationContext());

		mWeight = Integer.valueOf(pref.getValue("weight", ""));
		mBirth = pref.getValue("birth", "");
		isSexFlag = pref.getValue("sex", false);
		isAlarmFlag = pref.getValue("alarm", false);
		isVibrateFlag = pref.getValue("vibrate", false);
		
		TextView birth = (TextView)findViewById(R.id.birth_txt);
		TextView weight = (TextView)findViewById(R.id.weight_txt);
		
		birth.setText(mBirth);
		weight.setText(""+mWeight);

		final Button men = (Button)findViewById(R.id.men);
		final Button women = (Button)findViewById(R.id.women);
		ImageView alarm = (ImageView)findViewById(R.id.alarm);
		ImageView vibrate = (ImageView)findViewById(R.id.vibrate);
		alarm_setting = (RelativeLayout)findViewById(R.id.alarm_setting);
		
		if(isSexFlag){
			men.setBackgroundColor(Color.parseColor("#f3f3f3"));
			men.setTextColor(Color.parseColor("#48b7e8"));
			women.setBackgroundColor(Color.parseColor("#48b7e8"));
			women.setTextColor(Color.WHITE);
		}else{
			men.setBackgroundColor(Color.parseColor("#48b7e8"));
			men.setTextColor(Color.WHITE);
			women.setBackgroundColor(Color.parseColor("#f3f3f3"));
			women.setTextColor(Color.parseColor("#48b7e8"));
		}

		if(isAlarmFlag){
			alarm.setImageResource(R.drawable.setting_on);
			alarm_setting.setVisibility(View.VISIBLE);
		}else{
			alarm.setImageResource(R.drawable.setting_off);
			alarm_setting.setVisibility(View.GONE);
		}
		
		if(isVibrateFlag){
			vibrate.setImageResource(R.drawable.setting_on);
		}else{
			vibrate.setImageResource(R.drawable.setting_off);
		}
		
		men.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isSexFlag = !isSexFlag;
				if(isSexFlag){
					men.setBackgroundColor(Color.parseColor("#f3f3f3"));
					men.setTextColor(Color.parseColor("#48b7e8"));
					women.setBackgroundColor(Color.parseColor("#48b7e8"));
					women.setTextColor(Color.WHITE);
				}else{
					men.setBackgroundColor(Color.parseColor("#48b7e8"));
					men.setTextColor(Color.WHITE);
					women.setBackgroundColor(Color.parseColor("#f3f3f3"));
					women.setTextColor(Color.parseColor("#48b7e8"));
				}

				RbPreference pref = new RbPreference(getApplicationContext());
				pref.put("sex", isSexFlag);
				pref = null;
			}
		});
		
		women.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isSexFlag = !isSexFlag;
				if(isSexFlag){
					men.setBackgroundColor(Color.parseColor("#f3f3f3"));
					men.setTextColor(Color.parseColor("#48b7e8"));
					women.setBackgroundColor(Color.parseColor("#48b7e8"));
					women.setTextColor(Color.WHITE);
				}else{
					men.setBackgroundColor(Color.parseColor("#48b7e8"));
					men.setTextColor(Color.WHITE);
					women.setBackgroundColor(Color.parseColor("#f3f3f3"));
					women.setTextColor(Color.parseColor("#48b7e8"));
				}

				RbPreference pref = new RbPreference(getApplicationContext());
				pref.put("sex", isSexFlag);
				pref = null;
			}
		});
		
		alarm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isAlarmFlag = !isAlarmFlag;
				if(isAlarmFlag){
					alarm_setting.setVisibility(View.VISIBLE);
					((ImageView)v).setImageResource(R.drawable.setting_on);
					onAlarm();
				}else{
					alarm_setting.setVisibility(View.GONE);
					((ImageView)v).setImageResource(R.drawable.setting_off);
					offAlarm();
				}
				
				RbPreference pref = new RbPreference(getApplicationContext());
				pref.put("alarm", isAlarmFlag);
				pref = null;
			}
		});

		vibrate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isVibrateFlag = !isVibrateFlag;
				if(isVibrateFlag){
					((ImageView)v).setImageResource(R.drawable.setting_on);
				}else{
					((ImageView)v).setImageResource(R.drawable.setting_off);
				}
				
				RbPreference pref = new RbPreference(getApplicationContext());
				pref.put("vibrate", isVibrateFlag);
				pref = null;
			}
		});

		RelativeLayout alarm_setting = (RelativeLayout)findViewById(R.id.alarm_setting);
		alarm_setting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent(getApplicationContext(), AlarmActivity.class);
				startActivity(mIntent);
			}
		});
	}

/**
 ***************************************************************
 * Method
 ***************************************************************
 */
	
	//알람 등록
	private void onAlarm(){
		JSONArray objA = CodeUtil.getInstance(getApplicationContext()).getQuery("alarm");

		//등록
		for(int i=0; i<objA.length(); i++){
			try {
				//등록된 것들 해제.
				AlarmSchedule.getInstance(getApplicationContext()).deleteAlarmSchedule(objA.getJSONObject(i).getInt("idx"));
				
				AlarmSchedule.getInstance(getApplicationContext()).setAlarmSchedule(
						objA.getJSONObject(i).getInt("idx"),
						objA.getJSONObject(i).getInt("hour"),
						objA.getJSONObject(i).getInt("minute"));
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//등록취소
	private void offAlarm(){
		JSONArray objA = CodeUtil.getInstance(getApplicationContext()).getQuery("alarm");
		
		for(int i=0; i<objA.length(); i++){
			try {
				AlarmSchedule.getInstance(getApplicationContext()).deleteAlarmSchedule(
						objA.getJSONObject(i).getInt("idx"));
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
/**
 ***************************************************************
 * Event
 ***************************************************************
 */
	
	//클릭이벤트
	View.OnClickListener OCL = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.birth_layout:
			{
				WeightDialogFragment dpf = new WeightDialogFragment(StaticValue.DIALOG_BIRTH, mBirth);
				dpf.show(getSupportFragmentManager(), "");
				break;				
			}
			case R.id.weight_layout:
			{
				WeightDialogFragment dpf = new WeightDialogFragment(StaticValue.DIALOG_WEIGHT, mWeight);
				dpf.show(getSupportFragmentManager(), "");
				break;
			}
			default:
				break;
			}
		}
	};
	
	@Override
	public void onDialogNegativeClick(DialogFragment dialog) {
		// TODO Auto-generated method stub
		getData();
		dialog.dismiss();
	}

	@Override
	public void onDialogTimeClick(DialogFragment dialog, int idx, String hour, String minute) {
		// TODO Auto-generated method stub
		dialog.dismiss();
	}
	
}
